<?php

class __blogs_mail extends Module {
	
	public function __construct () {
		parent::__construct ('blogs');
	}
	
	/**
	 * Оповещение пользователей о новой публикации в некотором блоге
	 * @param Object $oPost объект публикации
	 * @return void
	 */
	public function postNotification (Object $oPost) {
		throw new CoreException ('method is fucking');
		
		$typesController = TypesController::getInstance ();
		$objectsController = ObjectsController::getInstance ();
		
		if (!$blogId = $oPost->getValue ('blog_id')) {
			return;
		}
		
		$oBlog = $objectsController->getObject ($typesController->getTypeByName ('blog'), $blogId);
		$oUserType = $typesController->getTypeByName ('user');
		
		// SPAM to blog members
		$sel = new Selection;
		$sel->addType ($typesController->getTypeByName ('user'));
		$sel->addSelectedProperty ('email');
		$sel->addSelectedProperty ('name');
		$sel->addSelectedProperty ('login');
				
		$res = $sel->getList ();
		
		foreach ($res as $row) {
			$template = new SimpleTemplate ('blogs/mailPostInBlog.tpl');
			$template->setVar ('{username}', $row['name']);
			$template->setVar ('{login}', $row['login']);
			$template->setVar ('{postType}', $oPost->getTypeName ());
			$template->setVar ('{postId}', $oPost->getId ());
			$template->setVar ('{blogAlias}', $oBlog->getValue ('alias'));
			$template->setVar ('{blogTitle}', $oBlog->getValue ('title'));
			
			Mailer::sendEMail(new EMail ($row['email'], $template->display (), lang('email_post_notif_subject','blogs') . $oBlog->getValue ('title')));
		}
	}
	
	/**
	 * Оповещение автора публикации о новом комментарии
	 * @param Object $oPost объект публикации
	 * @param Object $oComment объект нового комментария
	 * @return void
	 */
	public function postReplyNotification (Object $oPost, Object $oComment) {
		$typesController = TypesController::getInstance ();
		$objectsController = ObjectsController::getInstance ();
		
		if (ExplaySession::getUserId() == $oPost->getValue ('owner_id')) {
			return false;
		}
		
		$oPostAuthor = $objectsController->getObject ($typesController->getTypeByName ('user'), $oPost->getValue ('owner_id'));
		
		$oTypeSettings = $typesController->getTypeByName ('user_settings');
		$selUserSettings = new Selection;
		$selUserSettings->addType ($oTypeSettings);
		$selUserSettings->addFilter ('user_id', $oPostAuthor->getId ());
		$selUserSettings->addLimit (1);
		
		$res = $selUserSettings->getList ();
		if (count ($res)) {
			$oSettings = $objectsController->getObject ($oTypeSettings, $res[0][0]);
			if ($oSettings->getValue ('notification_postcomment') == 0) {
				return false;
			}
		}
		
		$oCurrentUser = ExplaySession::getUser ();
		
		$template = new SimpleTemplate ('blogs/mailPostReply.tpl');
		$template->setVar ('{postOwnerName}', $oPostAuthor->getValue ('name'));
		$template->setVar ('{postOwnerLogin}', $oPostAuthor->getValue ('login'));
		$template->setVar ('{commentOwnerLogin}', $oCurrentUser->getValue ('login'));
		$template->setVar ('{commentOwnerName}', $oCurrentUser->getValue ('name'));
		$template->setVar ('{commentId}', $oComment->getId ());
		$template->setVar ('{commentText}', $oComment->getValue ('text'));
		$template->setVar ('{postType}', $oPost->getTypeName ());
		$template->setVar ('{postId}', $oPost->getId ());
		$template->setVar ('{postTitle}', $oPost->getValue ('title'));
		
		Mailer::sendEMail(new EMail ($oPostAuthor->getValue ('email'), $template->display (), lang('email_post_reply_subject','blogs') . '"' . $oPost->getValue ('title') . '"'));
	}
	
	/**
	 * Оповещение пользователя об ответе на его комментарий
	 * @param Object $oPost объект публикации
	 * @param Object $oRelComment комментарий, на который отвечает текущий пользователь 
	 * @param Object $oReplyComment комментарий текущего пользователя
	 * @return void
	 */
	public function commentReplyNotification (Object $oPost, Object $oRelComment, Object $oReplyComment) {
		$typesController = TypesController::getInstance ();
		$oTypeComment = $typesController->getTypeByName ('comment');
		$objectsController = ObjectsController::getInstance ();
		
		if (ExplaySession::getUserId() == $oRelComment->getValue ('owner_id')) {
			return false;
		}
		
		$oCommentAuthor = $objectsController->getObject ($typesController->getTypeByName ('user'), $oRelComment->getValue ('owner_id'));
		
		$oTypeSettings = $typesController->getTypeByName ('user_settings');
		$selUserSettings = new Selection;
		$selUserSettings->addType ($oTypeSettings);
		$selUserSettings->addFilter ('user_id', $oCommentAuthor->getId ());
		$selUserSettings->addLimit (1);
		
		$res = $selUserSettings->getList ();
		if (count ($res)) {
			$oSettings = $objectsController->getObject ($oTypeSettings, $res[0][0]);
			if ($oSettings->getValue ('notification_comment') == 0) {
				return false;
			}
		}
		
		$oCurrentUser = ExplaySession::getUser ();
		
		$template = new SimpleTemplate ('blogs/mailCommentReply.tpl');
		$template->setVar ('{relCommentOwnerLogin}', $oCommentAuthor->getValue ('login'));
		$template->setVar ('{relCommentOwnerName}', $oCommentAuthor->getValue ('name'));
		$template->setVar ('{replyCommentOwnerLogin}', $oCurrentUser->getValue ('login'));
		$template->setVar ('{replyCommentOwnerName}', $oCurrentUser->getValue ('name'));
		$template->setVar ('{relCommentId}', $oRelComment->getId ());
		$template->setVar ('{replyCommentId}', $oReplyComment->getId ());
		$template->setVar ('{commentText}', $oReplyComment->getValue ('text'));
		$template->setVar ('{postType}', $oPost->getTypeName ());
		$template->setVar ('{postId}', $oPost->getId ());
		$template->setVar ('{postTitle}', $oPost->getValue ('title'));
		
		Mailer::sendEMail(new EMail ($oCommentAuthor->getValue ('email'), $template->display (), lang('email_comment_reply_subject','blogs')));
	}
	
}